﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;


namespace QLTV.Forms.BaoMat
{
    public partial class frmUser : BaseForm
    {
        public frmUser()
        {
            InitializeComponent();

        }
        public Boolean isNew { get; set; }
        public string username { get; set; }
        private void frmUser_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsUsers.tblPhong' table. You can move, or remove it, as needed.
            this.tblPhongTableAdapter.Fill(this.dsUsers.tblPhong);
            // TODO: This line of code loads data into the 'dsUsers.tblUsers' table. You can move, or remove it, as needed.
            this.tblUsersTableAdapter.Fill(this.dsUsers.tblUsers);
            // TODO: This line of code loads data into the 'dsUsers.tblRole' table. You can move, or remove it, as needed.
            this.tblRoleTableAdapter.Fill(this.dsUsers.tblRole);
            // TODO: This line of code loads data into the 'dsUsers.tblNhanSu' table. You can move, or remove it, as needed.
            this.tblNhanSuTableAdapter.Fill(this.dsUsers.tblNhanSu);
            if (isNew)
            {
                tblUsersBindingSource.AddNew();
            }
            else
                loadUserByUserName(username);

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                tblUsersBindingSource.EndEdit();
                tblUsersTableAdapter.Update(dsUsers.tblUsers);
                if (isNew)
                {
                    username = txtUserName.Text;
                    bool isAdmin = chkAdmin.Checked;
                    getPermission(username,isAdmin);
                    isNew = false;
                }

                
                this.Close();
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("unique"))
                {
                    MessageBox.Show("Tên tài khoản này đã tồn tại", "Thông báo");
                }
            }
        }
        private void loadUserByUserName(string _username)
        {
            tblUsersTableAdapter.FillByUserName(dsUsers.tblUsers, _username);
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {


            try
            {
                tblUsersBindingSource.EndEdit();
                tblUsersTableAdapter.Update(dsUsers.tblUsers);
                if (isNew)
                {
                    username = txtUserName.Text;
                    bool isAdmin = chkAdmin.Checked;
                    getPermission(username,isAdmin);
                    isNew = false;
                }


                tblUsersBindingSource.AddNew();

            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("unique"))
                {
                    MessageBox.Show("Tên tài khoản này đã tồn tại", "Thông báo");
                }
            }
        }

        private void luePhong_EditValueChanged(object sender, EventArgs e)
        {
            if (luePhong.EditValue.ToString() != "")
            {
                tblNhanSuTableAdapter.FillByPhongID(dsUsers.tblNhanSu, Convert.ToInt16(luePhong.EditValue));
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //lay quyen
        private void getPermission(string _username,Boolean isAdmin)
        {
            Database.dsUsersTableAdapters.tblItemsTableAdapter tadItem = new Database.dsUsersTableAdapters.tblItemsTableAdapter();
            Database.dsUsersTableAdapters.tblQuyenTableAdapter tadQuyen = new Database.dsUsersTableAdapters.tblQuyenTableAdapter();
            Database.dsUsers.tblQuyenDataTable dtQuyen = new Database.dsUsers.tblQuyenDataTable();
            foreach (Database.dsUsers.tblItemsRow rowItem in tadItem.GetData().Rows)
            {

                tadQuyen.getItem(_username,isAdmin, Convert.ToInt16(rowItem["iItemID"]), rowItem["cItemTitle"].ToString(), rowItem["cItemKey"].ToString(), Convert.ToInt16(rowItem["iParentId"]));

            }


        }

        private void tblUsersBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            isNew = true;
        }
    }
}